var express = require('express');
var router = express.Router();
var Gallery = require('../models/gallery.js');


//首页
router.get(/^\/$/, function (req, res, next) {

    Gallery.getList(1, '', res);

});

// /{PAGENUMBER}.html
router.get(/^\/(\d*)\.html$/, function (req, res, next) {

    var page = req.params[0];
    Gallery.getList(page, '', res);

});

// /{TAGS}_{PAGENUMBER}.html
router.get(/^\/([^\/d][^\/]*)_(\d*)\.html$/, function (req, res, next) {

    var tags = req.params[0] || null;
    var page = req.params[1];

    Gallery.getList(page, tags, res);

});

// /detail_{ID}.html
router.get(/^\/detail_([a-zA-Z0-9]*)\.html$/, function (req, res, next) {

    var id = req.params[0];
    Gallery.getDetail({
        id: id,
        from: null
    }, res);

});

// /detail_{TAGS}_{ID}.html
// 兼容错误
router.get(/^\/detail_([^_\.]*)_([a-zA-Z0-9]*)\.html$/, function (req, res, next) {

    var id = req.params[1];
    Gallery.getDetail({
        id: id,
        from: null
    }, res);

});

// /detail.php?id={ID}
// 兼容错误
//router.get(/\/detail\.php$/, function (req, res, next) {
//
//    var id = req.query.id;
//    Gallery.getDetail({
//        id: id,
//        from: null
//    }, res);
//
//});

module.exports = router;